#include "MainMenuState.h"

#include "../../Game.h"
#include "../GamePlay/GamePlayState.h"

#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"

CMainMenuState* CMainMenuState::GetInstance( void )
{
	static CMainMenuState s_Instance;

	return &s_Instance;
}

CMainMenuState::CMainMenuState(void)
{
	m_pD3D		= nullptr;
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pXA		= nullptr;

	m_sSongID				= 0;
	m_sSoundEffectID		= 0;

	m_sCursorPosition		= 0;
}

CMainMenuState::~CMainMenuState(void)
{
	m_pD3D		= nullptr;
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pXA		= nullptr;

	m_sSongID				= 0;
	m_sSoundEffectID		= 0;

	m_sCursorPosition		= 0;
}

void CMainMenuState::Enter( void )
{
	m_pD3D		= CSGD_Direct3D::GetInstance();
	m_pDI		= CSGD_DirectInput::GetInstance();
	m_pTM		= CSGD_TextureManager::GetInstance();
	m_pXA		= CSGD_XAudio2::GetInstance();

	m_sSongID				= 0;
	m_sSoundEffectID		= 0;

	m_pXA->MusicSetMasterVolume	( m_pXA->MusicGetMasterVolume() );
	m_pXA->SFXSetMasterVolume	( m_pXA->SFXGetMasterVolume() );

	m_sCursorPosition		= 60;
}

void CMainMenuState::Exit( void )
{
	//m_pXA->MusicStopSong	( m_sSongID );
	//m_pXA->MusicUnloadSong	( m_sSongID );

	//m_pXA->SFXStopSound		( m_sSoundEffectID );
	//m_pXA->SFXUnloadSound	( m_sSoundEffectID );

	m_pD3D	= nullptr;
	m_pDI	= nullptr;
	m_pTM	= nullptr;
	m_pXA	= nullptr;

	m_sSongID			= 0;
	m_sSoundEffectID	= 0;

	m_sCursorPosition	= 0;
}

bool CMainMenuState::Input( void )
{
	if( m_pDI->KeyPressed( DIK_W ) == true || m_pDI->KeyPressed( DIK_UP ) == true )
	{
		m_sCursorPosition -= 40;

		if( m_sCursorPosition < 60 )
			m_sCursorPosition = 180;
	}
	else if( m_pDI->KeyPressed( DIK_S ) == true || m_pDI->KeyPressed( DIK_DOWN ) == true )
	{
		m_sCursorPosition += 40;

		if( m_sCursorPosition > 180 )
			m_sCursorPosition = 60;
	}

	if( m_pDI->KeyPressed( DIK_RETURN ) == true )
	{
		switch( m_sCursorPosition )
		{
		case 60:
			{
				CGame::GetInstance()->ChangeState( CGamePlayState::GetInstance() );
			}
			
		}
	}
	return true;
}

void CMainMenuState::Update( float fElapsedtime )
{
	


}

void CMainMenuState::Render( void )
{
	m_pD3D->DrawText( _T( ">"), 350, m_sCursorPosition, 255, 255, 255);

	m_pD3D->DrawText( _T("TRI-QUEST"), 350, 20, 255, 255, 255 );
	m_pD3D->DrawText( _T("Play"), 365, 60, 255, 255, 255 );
	m_pD3D->DrawText( _T("Options"), 365, 100, 255, 255, 255 ); 
	m_pD3D->DrawText( _T("Credits"), 365, 140, 255, 255, 255 );
	m_pD3D->DrawText( _T("Exit"), 365, 180, 255, 255, 255 );
}
